'use strict';

const Controller = require('egg').Controller;

class SearchController extends Controller {
    // 热门搜索
  async hot() {
      const { ctx, app } = this;
      let newtime = new Date(), Op = app.Sequelize.Op;
      let list = await app.model.Hot.findAll({
          where: {
              statr_time: {
                  [Op.lte]:newtime
              },
              end_time: {
                  [Op.gte]:newtime
              }
          }
      })
      ctx.apiSuccess(list)
    }
    // 搜索 门店
    async searchShop() {
        const { ctx, app } = this;
        const { title } = ctx.request.body
        ctx.validate({
            title: {
                type: 'string',
                required: true,
                desc: '搜索内容'
            }
        })
        let Op = app.Sequelize.Op
        let list = await app.model.Shop.findAndCountAll({
            where: {
                title: {
                    [Op.like]:'%'+title+'%'
                }
            },
            include: [
                {
                    model: app.model.Coupons,
                    attributes: ['title']
                }]
        })
        ctx.apiSuccess(list)
    }
}

module.exports = SearchController;
